import { _decorator, Component, macro, ResolutionPolicy, screen, view } from 'cc';
import { DataMgr } from './manager/DataMgr';
import { ENUM_RESOURCES, ENUM_UI_LAYER } from './Enum';
import { ResMgr } from './manager/ResMgr';
import { Ins } from './Ins';
import { AudioMgr } from './manager/AudioMgr';
import { SdkMgr } from './manager/SdkMgr';
const { ccclass, property } = _decorator;

@ccclass('Index')
export class Index extends Component {
    onLoad() {
        this.resize()
        view.setResizeCallback(() => this.resize())
        DataMgr.ins.loadingRange = 0
        macro.ENABLE_MULTI_TOUCH = false
    }

    protected async start() {
        // 初始化资源
        for (const i in ENUM_RESOURCES) {
            const res = ENUM_RESOURCES[i]
            await ResMgr.ins.init(res)
        }
        // 初始化ui
        Ins.UIMgr.init()
        // 读档
        DataMgr.ins.restore()
        // 背景音乐
        AudioMgr.ins.playMusic()
        // 初始化sdk
        SdkMgr.ins.initBannerAd()
        SdkMgr.ins.initInterstitialAd()
        SdkMgr.ins.initVideoAd()
        SdkMgr.ins.initCustomColAd()
        SdkMgr.ins.initCustomRowAd()
        SdkMgr.ins.initAudioEndListener()
        SdkMgr.ins.passiveShare()
        // 载入首页
        Ins.UIMgr.toggle(ENUM_UI_LAYER.MENU, true, () => {
            DataMgr.ins.loadingRange = 1
        })
    }

    resize() {
        const design = view.getDesignResolutionSize()
        const window = screen.windowSize
        const setFitWidth = () => {
            view.setResolutionPolicy(ResolutionPolicy.FIXED_WIDTH)
        }

        const setFitHeight = () => {
            view.setResolutionPolicy(ResolutionPolicy.FIXED_HEIGHT)
        }

        const setFitBoth = () => {
            view.setResolutionPolicy(ResolutionPolicy.SHOW_ALL)
        }
        const designR = design.width / design.height
        const windowR = window.width / window.height
        if (designR < 1) {
            // console.log('-----【竖屏】')
            if (windowR < 1) {
                if (windowR > designR) {
                    setFitBoth()
                } else {
                    setFitWidth()
                }
            } else {
                setFitBoth()
            }
        } else {
            // console.log('-----【横屏】')
            if (windowR > 1) {
                if (windowR < designR) {
                    setFitBoth()
                } else {
                    setFitHeight()
                }
            } else {
                setFitBoth()
            }
        }
    }
}


